<?php

namespace platform;

/**
 * 阿旗平台管理
 */
class AqiInfoPlatform
{
    //申请获得的appKey
    public string $sAppKey = '592f1c4ccd4396bcc494baf4794b227e';
    //申请获得的secret
    public string $sSecret = 'ec6acc79c1bb6724060fec17914f6ad9';

    /**
     * 查询接口
     * @param string $uuid 渠道方用户唯一ID,不能大于32个字符
     * @param string $extra 额外参数，会在成功通知接口原文回传。例如，成功跳转链接将会有透传参数 http://aaaa.aaa.com?extra=?????
     * @param string $vmid 渠道方提供的机器编号
     * @param int $sex 用户性别 0 未知，1男，2女
     * @param string $city 用户微信城市，末尾不包含“市”字。例如：深圳
     */
    public function queryInterface(string $uuid, string $extra, string $vmid, int $sex = 0, string $city = '深圳'): array
    {
        $sApi = 'https://c.api.aqiinfo.com/ChannelApi/UfansTicket';
        $aData = [
            'appKey' => $this->sAppKey,
            'exUid' => $uuid,
            'extra' => $extra,
            'vmid' => $vmid,
            'sex' => $sex,
            'city' => $city,
            'time' => time(),
        ];
        $aData['ufsign'] = $this->sign($aData, $this->sSecret);
        //发送请求
        try {
            $sRes = $this->post($sApi, $aData);
            $sRes = json_decode($sRes, true);
            if ($sRes['code'] === 200) {
                return ['bool' => true, 'data' => $sRes['data'], 'msg' => $sRes['msg']];
            }
            return ['bool' => false, 'data' => [], 'msg' => $sRes['msg']];
        } catch (\Exception $exception) {
            return ['bool' => false, 'data' => [], 'msg' => $exception->getMessage()];
        }
    }

    /**
     * 发送httpPOST请求
     * @param string $url 请求地址
     * @param array $aParams 请求参数 [ key => value ]
     * @param integer $iTimeout 请求超时限制
     * @return string,false 请求结果，false 为通讯异常
     */
    private function post(string $url, array $aParams, int $iTimeout = 3): string
    {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== false) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        $sParams = http_build_query($aParams);
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $sParams);
        curl_setopt($oCurl, CURLOPT_TIMEOUT, $iTimeout);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

    /**
     * 构建签名字符
     * @param array $aData 生成签名对应的参数
     * @param string $sSecret 签名秘钥
     * @return string,false 签名字符串 false 为签名无效
     */
    private function sign(array $aData, string $sSecret): string
    {
        if (empty($aData)) {
            return false;
        }
        ksort($aData);
        $aQuery = [];
        foreach ($aData as $sKey => $sVal) {
            if ($sKey == 'ufsign') {
                continue;
            }
            $aQuery[] = "{$sKey}={$sVal}";
        }
        $sSignStr = implode("&", $aQuery);
        $sStr = hash_hmac('sha1', $sSignStr, $sSecret, true);
        return md5($sStr);
    }


}